<template>
  <div>
    <!-- 第一行  -->
    <div class="header mb-4">
      <el-form :inline="true" :model="form" class="demo-form-inline">
        <el-form-item>
          <el-select clearable v-model="form.online_status" placeholder="请选择设备在线状态">
            <el-option label="在线" :value="1"></el-option>
            <el-option label="离线" :value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select clearable v-model="form.bind_status" placeholder="请选择设备绑定状态">
            <el-option label="已绑定" :value="1"></el-option>
            <el-option label="未绑定" :value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input clearable v-model="form.name" placeholder="请输入设备名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="toSearch">搜索</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="toAdd">录入</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 第二行  -->
    <div class="table">
      <el-table :data="deviceData" style="width: 100%" border>
        <el-table-column type="index" label="编号" width="80">
        </el-table-column>
        <el-table-column prop="serial_number" label="设备编号" width="180">
        </el-table-column>
        <el-table-column prop="name" label="设备名称">
        </el-table-column>
        <el-table-column prop="online_status" label="在线状态">
          <template v-slot="scope">
            <el-tag v-if="scope.row.online_status === 1" type="success">在线</el-tag>
            <el-tag v-else type="danger">离线</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="bind_status" label="绑定状态">
          <template v-slot="scope">
            <el-tag v-if="scope.row.bind_status === 1" type="success">已绑定</el-tag>
            <el-tag v-else type="danger">未绑定</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template v-slot="scope">
            <el-button size="small" type="primary" @click="toEdit(scope.row)">修改</el-button>
            <el-button size="small" type="danger" @click="toDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 编辑弹窗 -->
    <el-dialog :title="title" :visible.sync="visible" width="50%">
      <el-form :rules="rules" :model="editForm" ref="editForm" label-width="100px">
        <el-form-item label="设备编号" prop="serial_number">
          <el-input v-model="editForm.serial_number"></el-input>
        </el-form-item>
        <el-form-item label="设备名称" prop="name">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="save">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { get, put, del, postJSON } from '@/utils/request'
export default {
  data() {
    return {
      // 表单数据对象
      form: {},
      // 设备数据
      deviceData: [],
      // 分页参数
      params: {
        page: 1,
        pageSize: 10
      },
      title:'新增设备',
      // 新增弹窗相关
      visible: false,
      editForm: {
      },
      rules: {
        serial_number: [
          { required: true, message: '请输入设备编号', trigger: "blur" },
        ],
        name: [
          { required: true, message: '请输入设备名称', trigger: "blur" },
        ],
      }
    }
  },
  methods: {
    toAdd(){  
      this.title = '新增设备';
      this.editForm = {};
      this.visible = true;
    },
    // 搜索
    toSearch() {
      this.params.page = 1;
      this.getDevData();
    },
    //新增修改设备
    async save() {
      try {
        // 表单验证
        await this.$refs.editForm.validate();
        // 调用新增API
        const res = await postJSON('/device/saveOrUpdate', this.editForm);
        if(res.status==200){
          this.$message({
            type:"success",
            message:res.message
          })
          this.visible = false;
          this.getDevData()
        }
      } catch (error) {
        if (error.name === 'ValidationError') return; // 表单验证失败不处理
        this.$message.error('操作失败，请重试');
        console.error('新增失败', error);
      }
    },
    // 修改设备
    toEdit(row) { // 补充参数接收
      this.title = '修改设备'
      this.editForm = { ...row }; // 赋值当前行数据
      this.visible = true;
    },
    // 删除设备
    async toDelete(row) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 点了确定按钮 发起删除工程请求 
        let res = await del('/device/deleteById', { id:row.id });
        this.$message({
          type: 'success',
          message: '操作成功!'
        });
        // 从第一页重新载入数据 
        this.params.page = 1;
        // 重新载入数据 
        this.getDevData();
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        });
      });
    },
    // 获取设备数据
    async getDevData() {
      let temp = {
        ...this.form,
        ...this.params
      };
      let res = await get('/device/pageQuery', temp);
      this.deviceData = res.data.list || [];
    }
  },
  created() {
    this.getDevData();
  }
}
</script>
